# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

#_wx_basever=3.2

_realname=openbabel
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.1.1
pkgrel=1
pkgdesc="A chemical toolbox designed to speak the many languages of chemical data. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://openbabel.org'
msys2_repository_url='https://github.com/openbabel/openbabel'
msys2_references=(
  'archlinux: openbabel'
)
license=('spdx:GPL-2.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-coordgen"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-maeparser")
             #"${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw")
optdepends=("${MINGW_PACKAGE_PREFIX}-cairo: PNG file format"
            "${MINGW_PACKAGE_PREFIX}-maeparser: Maestro file format")
            #"${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs: GUI")
source=("https://github.com/openbabel/openbabel/releases/download/openbabel-${pkgver//./-}/openbabel-${pkgver}-source.tar.bz2"
        "001-fix-build-on-mingw.patch")
sha256sums=('a6ec8381d59ea32a4b241c8b1fbd799acb52be94ab64cdbd72506fb4e2270e68'
            'fb00e5119e573f5b6f4f149ed2a61797fcb87f4ed852668791cd815b9b919b14')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_GUI=OFF \
      -DENABLE_OPENMP=ON \
      -DWITH_COORDGEN=ON \
      -DWITH_JSON=ON \
      -DWITH_MAEPARSER=ON \
      -DOPTIMIZE_NATIVE=OFF \
      -DENABLE_TESTS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

check() {
  "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
    -Bbuild-${MSYSTEM} \
    -S${_realname}-${pkgver} \
    -DENABLE_TESTS=ON
  "${MINGW_PREFIX}"/bin/cmake.exe --build build-${MSYSTEM}

  PATH="${srcdir}"/build-${MSYSTEM}:$PATH \
    "${MINGW_PREFIX}"/bin/ctest --test-dir build-${MSYSTEM} || echo "Tests failed"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/openbabel3/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
